package com.grieous.wow.biz.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grieous.wow.biz.FamilyBiz;
import com.grieous.wow.common.DateUtil;
import com.grieous.wow.dao.impl.FamilyDaoImpl;
import com.grieous.wow.entity.Family;
import com.grieous.wow.entity.FamilyVote;
import com.grieous.wow.entity.PageModel;

@Service("familyBiz")
@Transactional
public class FamilyBizImpl implements FamilyBiz {
	@Resource
	private FamilyDaoImpl familyDao;

	@Override
	public PageModel getFamily(Integer pageSize, Integer page,
			String orderCondition, String status) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = 0;
		if (pageSize != null) {
			page = page == null ? 1 : page;
			count = familyDao.findCountByCondition(map);
			map.put("startId", (page - 1) * pageSize);
			map.put("pageSize", pageSize);
		}
		map.put("orderCondition", orderCondition);
		map.put("status", status);
		List<Family> list = familyDao.findListByCondition(map);

		PageModel pm = PageModel.getPageModel(pageSize, page, count, list);
		return pm;
	}

	@Override
	public boolean saveFamily(Family family) {
		if (family != null) {
			if (family.getId() != null && !family.getId().equals("")) {
				if (familyDao.update(family) == 1)
					return true;
			} else {
				if (familyDao.add(family) == 1)
					return true;
			}
		}
		return false;
	}

	@Override
	public boolean delFamily(int... ids) {
		if (ids != null && ids.length > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ids", ids);
			if (familyDao.delete(map) == ids.length)
				return true;
		}
		return false;
	}

	@Override
	public Family getFamilyById(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		List<Family> list = familyDao.findListByCondition(map);
		if (list != null && list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public String voteFamily(Integer familyId, String ip) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("familyId", familyId);
		map.put("ip", ip);
		map.put("voteTime",
				DateUtil.getDateStr(DateUtil.PATTERN_DAY, new Date()));
		int count = familyDao.getFamilyVoteCount(map);
		if (count != 0) {
			return "你今天已经投过一次了！";
		} else {
			Family family = getFamilyById(familyId);
			if (family != null) {
				FamilyVote familyVote = new FamilyVote();
				familyVote.setFamilyId(familyId);
				familyVote.setIp(ip);
				familyDao.insertFamilyVote(familyVote);
				family.setClicks(family.getClicks() + 1);
				familyDao.update(family);
				return "你已投票成功！";
			}
			return "";

		}
	}

	@Override
	public boolean checkFamilyName(String name) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		int count = familyDao.findCountByCondition(map);
		if (count > 0) {
			return false;
		}
		return true;
	}
}
